﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {
    public CharacterController cc;//组件
    public float movespeed;
    public float jumpspeed;
    public float gravity;//重力
    public Transform groundcheck;//落地判定位置
    public float checkradius;//落地判定半径
    public LayerMask groundlayer;//落地判定层级
    public bool isGround;//判定落地
    private float HM, VM;//按键
    private Vector3 move;//三维方向
    private Vector3 G;
    
    private void Star()
    {
        cc = GetComponent<CharacterController>();//获取组件
    }
    private void Update()
    {
        isGround = Physics.CheckSphere(groundcheck.position, checkradius, groundlayer);
        if(isGround && G.y < 0)
        {
            G.y = -2f;
        }
        //获取按键值
        HM = Input.GetAxis("Horizontal") * movespeed;
        VM = Input.GetAxis("Vertical") * movespeed;
        move = transform.forward * VM + transform.right * HM;//方向信息
        cc.Move(move * Time.deltaTime);//移动
        if (Input.GetButtonDown("Jump"))
        {
            G.y = jumpspeed;
        }
        G.y -= gravity * Time.deltaTime;
        cc.Move(G * Time.deltaTime);
    }
}
